package com.common.controller;

import com.common.entity.FileStorageVO;
import com.common.enums.ApiResult;
import com.common.service.FileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping
public class FileUploadController {

    @Autowired
    private FileStorageService fileStorageService;

    @PostMapping("/upload")
    public ResponseEntity<ApiResult> uploadFile(@RequestParam("file") MultipartFile file) {
        //检查文件是否为空
        if(file.isEmpty()){
            return ResponseEntity
                    .badRequest()
                    .body(ApiResult.failure("文件不能为空"));
        }
        //调用文件存储服务保存文件
        FileStorageVO filename = fileStorageService.storeFile(file);
        //返回成功响应
        return ResponseEntity
                .ok()
                .body(ApiResult.success(filename));
    }

}
